import dayjs from "dayjs"

//储存
const $setStorage = (key, value) => {
  uni.setStorageSync(key, value)
}
//获取
const $getStorage = (key) => {
  return uni.getStorageSync(key)
}
//toast提示
const $toast = (msg, duration = 2000) => {
  uni.showToast({
    title: msg,
    icon: "none",
    duration,
  })
}

//showLoading
const $showLoading = (msg = "加载中") => {
  uni.showLoading({
    title: msg,
  })
}

//时间格式化
const $Date = (date, format = "YYYY-MM-DD HH:mm:ss") => {
  return dayjs(date).format(format)
}

//传递参数格式化
const $setQ = (query) => {
  if (!query) return
  query = JSON.stringify(query)
  return encodeURIComponent(query)
}

//获取参数
const $getQ = (query) => {
  if (!query) return
  query = decodeURIComponent(query)
  return JSON.parse(query)
}

export default {
  $setStorage,
  $getStorage,
  $toast,
  $showLoading,
  $Date,
  $setQ,
  $getQ,
}
